package com.dushibao.service.impl;

import com.dushibao.dao.MenuDao;
import com.dushibao.entity.Menu;
import com.dushibao.service.MenuService;
import com.dushibao.util.Page;
import com.dushibao.util.ResultUtils;
import com.dushibao.util.SecurityHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MenuServiceImpl implements MenuService {
    MenuDao menuDao = new MenuDao();

    @Override
    public List<Menu> list() {
        return menuDao.list();
    }

    @Override
    public String page() {
        HttpServletRequest req = SecurityHolder.request.get();
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        String menuName = req.getParameter("menuName");
        String menuUrl = req.getParameter("menuUrl");

        Page page = new Page();
        page.setCurrent(Integer.parseInt(current==null?"1":current));
        page.setPageSize(Integer.parseInt(pageSize==null?"10":pageSize));

        Page iPage =  menuDao.page(page,menuName,menuUrl);

        Map<String,Object> maps = new HashMap<>();
        maps.put("iPage",iPage);
        maps.put("menuName",menuName);
        maps.put("menuUrl",menuUrl);

        return ResultUtils.success(maps);
    }

    @Override
    public Menu getById(long id) {
        return menuDao.getById(id);
    }

    @Override
    public Integer saveOrUpdate(Menu menu) {
        if(menu.getId()!=null){
            return menuDao.update(menu);
        }

        return menuDao.save(menu);
    }

    @Override
    public Integer deleteById(List<Long> ids) {
        return menuDao.deleteById(ids);
    }

}
